package com.helei.common.api;

import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.HashMap;
import java.util.Map;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Result {
    private Integer code;
    private String desc;
    private Object data;
    private Object exta;

    public static Object okPage(IPage page) {
        return getResult(page);
    }
    public static Object okPage(IPage page, Object exta) {
        Result result = getResult(page);
        result.setExta(exta);
        return result;
    }

    private static Result getResult(IPage page) {
        Result result = new Result();
        if (page != null) {
            Map<String, Object> data = new HashMap<>(5);
            data.put("list", page.getRecords());
            data.put("total", page.getTotal());
            data.put("page", page.getCurrent());
            data.put("size", page.getSize());
            data.put("pages", page.getPages());
            result.setData(data);
        }
        return result;
    }

    public static Result ok(Object data) {
        Result result = new Result();
        result.setData(data);
        result.setCode(ResultCode.SUCCESS.getCode());
        return result;
    }
    public static Result ok(Object data,Object exta) {
        Result result = new Result();
        result.setData(data);
        result.setExta(exta);
        result.setCode(ResultCode.SUCCESS.getCode());
        return result;
    }

    public static Result ok() {
        return ok(null);
    }

    public static Result er() {
        return Result.er("请求错误");
    }

    public static Result er(int code) {
        Result result = new Result();
        result.setCode(code);
        return result;
    }

    public static Result er(String desc) {
        Result result = new Result();
        result.setDesc(desc);
        result.setCode(ResultCode.FAILED.getCode());
        return result;
    }

    public static Result er(int code, String desc) {
        return new Result(code, desc, null, null);
    }
}
